package ch.il07.converter.presentation.beans.entities;

import java.util.ArrayList;
import java.util.List;

import ch.il07.converter.business.ServiceFactoryAdapter;
import ch.il07.converter.business.UnitAction;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.converter.presentation.Helper;

public class MeasurandBean {
	private String name = null;
	private String oldName = null;
	private String defaultUnit = null;
	private String description = null;
	private String owner = null;
	private List<String> units = new ArrayList<String>();
	private boolean isPublic = false;
	private boolean selected = false;
	
	/**
	 * Creates an instance of MeasurandBean, mainly used for the Controller part in the web GUI.
	 * @param name 
	 * @param defaultUnit
	 * @param description
	 */
	public MeasurandBean(String owner, String name, String defaultUnit, String description) {
		this.owner = owner;
		this.name = name;
		this.oldName = name;
		this.defaultUnit = defaultUnit;
		this.description = description;
		this.units = fillUnits();
		this.isPublic = (owner == null);
	}
	
	@Override
	public String toString() {
		return owner + ": " + name + " (" + defaultUnit + ", " + description + ")";
	}
	
	private List<String> fillUnits() {
		List<String> units = new ArrayList<String>();
		UnitAction ua = ServiceFactoryAdapter.getUnitActionInstance();
		
		for(Unit u : ua.getPrivateUnitsMeasurand(name, Helper.getUserName())) {
			units.add(u.getName());
		}
		
		for(Unit u: ua.getPublicUnitsMeasurand(name)) {
			units.add(u.getName());
		}
		
		return units;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the defaultUnit
	 */
	public String getDefaultUnit() {
		return defaultUnit;
	}
	/**
	 * @param defaultUnit the defaultUnit to set)
	 */
	public void setDefaultUnit(String defaultUnit) {
		this.defaultUnit = defaultUnit;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the units
	 */
	public List<String> getUnits() {
		return units;
	}

	/**
	 * @param units the units to set
	 */
	public void setUnits(List<String> units) {
		this.units = units;
	}

	/**
	 * @return the isSelected
	 */
	public boolean isSelected() {
		return selected;
	}
		/**
	 * @param isSelected the isSelected to set
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	/**
	 * @return the owner
	 */
	public String getOwner() {
		return owner;
	}

	/**
	 * @param owner the owner to set
	 */
	public void setOwner(String owner) {
		this.owner = owner;
	}

	/**
	 * @return the oldName
	 */
	public String getOldName() {
		return oldName;
	}

	/**
	 * @param oldName the oldName to set
	 */
	public void setOldName(String oldName) {
		this.oldName = oldName;
	}

	/**
	 * @return the isPublic
	 */
	public boolean isPublic() {
		return isPublic;
	}

	/**
	 * @param isPublic the isPublic to set
	 */
	public void setPublic(boolean isPublic) {
		this.isPublic = isPublic;
	}
}
